<!DOCTYPE html>
<html>
<head>
	<div th:include="base-libs-easyui :: base-libs-easyui" />
<title>机构科室信息</title>
<style type="text/css">
	body {
		padding: 0px 10px;
	}
	.form-table input{
		width: 500px;
		padding-left: 4px;
	}
	.form-table select{
		width: 504px!important;
	}
	.tree-icon{
		display: none;
	}

	.bt-selected{
		background-color: #D0E9C6;
	}
</style>
</head>
<script th:inline="javascript">
    /**读取request中的初始化数据*/
    var requestData = [[${requestData}]];
</script>
<script type="text/javascript">
	$(function() {
		initView();
		queryBzksxx();
	});

	/**查询标准科室值域*/
	function queryBzksxx(){
	    var uri = zydmService + "/ksxx/queryAll";
        $.QuickRemote.AjaxJson(uri, queryBzksxxHandler);
	}
	function queryBzksxxHandler(result){
		var ary = $.QuickEasyUIUtils.createTreeList(result, "", "XMDM", "XMMC", "SJDM");
        $("#PTKSDM").combotree("loadData", ary);
        initData();
	}

	function initData() {
	    var updatedJgksxx = StoreCache.removeCache("curr_jgksxx");
        if(updatedJgksxx)
        {
            $("#formJgKsxx").data("updatedJgksxx", updatedJgksxx);
			/*修改禁用医疗机构下拉框*/
            $("#combogridYljg").combogrid("readonly", true);
            /*修改禁用科室代码*/
            $("#KSDM").attr("readonly", true);
            setData(updatedJgksxx);
        }else if(!requestData["isManager"]){
			var PTJGID = requestData["PTJGID"];
			var YLJGMC = requestData["YLJGMC"];
			var value = {PTJGID: PTJGID, YLJGMC: YLJGMC};
			$("#combogridYljg").combogrid("grid").datagrid("loadData", [value]);
			$("#combogridYljg").combogrid("setValue", value);
		}
	}

	function setData(){
	    var updatedJgksxx = $("#formJgKsxx").data("updatedJgksxx");
        $(".form-control").each(function(){
            var name = $(this).attr("name");
            if(!StringUtils.isEmpty(name)){
                var value = updatedJgksxx[name];
                $(this).QuickField("setData", value);
			}
        })
		/*医疗机构赋值*/
        $("#combogridYljg").combogrid("grid").datagrid("loadData", [{PTJGID: updatedJgksxx["PTJGID"], YLJGMC: updatedJgksxx["YLJGMC"]}]);
        $("#combogridYljg").combogrid("setValue", {PTJGID: updatedJgksxx["PTJGID"], YLJGMC: updatedJgksxx["YLJGMC"]});
		/*标准科室赋值*/
        $("#PTKSDM").combotree("setValue", {id: updatedJgksxx["PTKSDM"]});
        /*特色科室*/
        if(updatedJgksxx["SFTSKS"] == "1"){
            $("#SFTSKS").switchbutton("check");
		}
	}

	function initView() {
		$("#formJgKsxx").attr("action", contextRoot + "/ksxx/saveKsxx");
        $("#PTKSDM").combotree({
            panelHeight: 160
			,height: 30
            ,editable: false
            ,textField: "XMMC"
        });
        $("#SFTSKS").switchbutton({
            onText: "是"
            ,offText: "否"
		})
		$("#btnSave").on("click", btnSaveHandler);
        $("#btnClose").on("click", btnCloseHandler);

        var readonly = !requestData["isManager"];
        $("#combogridYljg").combogrid({
			panelHeight: 200
            ,height: 30
            ,singleSelect: true
            ,pagination: true
            ,idField: 'PTJGID'
            ,textField: 'YLJGMC'
            ,mode: "remote"
            ,url: contextRoot + "/yljgxx/queryPageSummary.service"
			,readonly: readonly
            ,hasDownArrow: !readonly
			,delay: 1000
            ,reversed: true
            ,columns:[[
                {
                    field:'YLJGMC'
                    ,title:'机构名称'
                    ,width:240
                }
                ,{
                    field:'XZQHMC'
                    ,title:'行政区划'
                    ,width:80
                }
                ,{
                    field:'WSJGLBMC'
                    ,title:'机构类别'
                    ,width:80
                }
                ,{
                    field:'YYDJMC'
                    ,title:'机构等级'
                    ,width:80
                }
            ]]
            ,onBeforeLoad: function(queryParams) {
                var url = $(this).datagrid("options")["url"];
				/*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    queryParams["YLJGMC"] = queryParams["q"];
                    return true;
                }else{
                    return false;
                }
            }
			,onChange: function(newValue, oldValue){
                /*响应改变事件*/
                var value = $("#combogridYljg").combogrid("grid").datagrid("getSelected");
                if(value){
                    $("#PTJGID").val(value["PTJGID"]);
                    $("#YLJGDM").val(value["YLJGDM"]);
				}else{
                    $("#PTJGID").val("");
                    $("#YLJGDM").val("");
				}
			}
        });
	}

	function btnSaveHandler(){
        if($(".form-control").QuickValidate())
        {
            var method = "add";
            var old = $("#formJgKsxx").data("updatedJgksxx");
            if(old){
                method = "update";
			}
            var params = {method: method};
            /*获取是否特色科室*/
            params["SFTSKS"] = getSFTSKS();
            /*默认一级科室*/
            params["JB"] = "1";

            $.QuickRemote.AjaxFiledFormSubmit("formJgKsxx", saveHandler, params);
        }
	}
	function saveHandler(result){
        var state = result["state"];
        if(state)
        {
            $.QuickAlert.alertNormal("保存成功", {callback: function(){
                $("#btnClose").trigger("click");
            }});
        }
        else
        {
            var info = result["info"];
            $.QuickAlert.alertFail({content: info["faultInfo"]});
        }
	}

	/**获取特色科室*/
	function getSFTSKS(){
	    var SFTSKS = "0";
	    var checked = $("#SFTSKS").switchbutton("options")["checked"];
	    if(checked){
            SFTSKS = "1";
		}
		return SFTSKS;
	}

	/**验证机构*/
	function validateJg(){
        var item = $("#combogridYljg").combogrid("grid").datagrid("getSelected");
        if(!item){
            return "请选择机构";
		}else{
            return "";
		}
	}
	/**验证标准科室*/
	function validateBzks(){
        var item = $("#PTKSDM").combotree("tree").tree("getSelected");
        if(!item){
            return "请选择标准科室";
        }else{
            return "";
        }
	}

	/**返回按钮*/
	function btnCloseHandler() {
        parent.layer.closeAll();
	}
</script>

<body class="easyui-layout">
	<div region="center" style="border: none;">
		<form id="formJgKsxx">
			<table class="form-table">
				<tr>
					<td class="td-text">
						<label>机构名称</label>
					</td>
					<td class="td-content">
						<input id="combogridYljg" class="form-control" name="PTJGID"
							   quick-config="label:'机构名称', validateProxy:'validateJg', length: '64'">
						<input id="YLJGDM" name="YLJGDM" class="form-control" style="display: none;">
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>标准科室名称</label>
					</td>
					<td class="td-content">
						<select id="PTKSDM" name="PTKSDM" class="form-control"
								quick-config="label:'标准科室名称', validateProxy:'validateBzks', length: '64'">
						</select>
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>科室代码</label>
					</td>
					<td class="td-content">
						<input id="KSDM" name="KSDM" type="text" placeholder="科室代码"
							   quick-config="label:'科室代码', validateProxy:'notNull length', length: '64'"
							   class="form-control">
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>科室名称</label>
					</td>
					<td class="td-content">
						<input id="KSMC" name="KSMC" type="text" placeholder="科室名称"
							   quick-config="label:'科室名称', validateProxy:'notNull length', length: '64'"
							   class="form-control">
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>是否特色科室</label>
					</td>
					<td class="td-content">
						<input id="SFTSKS" class="easyui-switchbutton">
					</td>
				</tr>
			</table>
		</form>
	</div>
	<div region="south" style="height: 41px; padding-top: 6px;">
		<button id="btnClose" type="button" style="margin-right: 10px; float: right;">关闭</button>
		<button id="btnSave" type="button" style="margin-right: 10px; float: right;">保存</button>
	</div>
</body>
</html>


